﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.ServiceModel;
using System.Runtime.Serialization;
using HF.Framework.SqlDataContract;
using HF.Framework.DataClient;

namespace HF.WCF.DBService
{
   [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
   public  class DatabaseService:IDataBaseService
    {
        private bool _isTrans;
        private IDatabase _dataBase;
        public bool IsTrans
        {
            get { return _isTrans; }
            set { _isTrans = value; }
        }
        public IDatabase Database
        {
            get { return _dataBase; }
            set { _dataBase = value; }
        }

        public DatabaseService()
        {
            _dataBase = new DataProvider();


        }

        public int ExecuteNonQuery(SqlDataItem sqlDataItem)
       {
           return _dataBase.ExecuteNonQuery(sqlDataItem);
       }

        public DataSet ExecuteQuery(SqlDataItem sqlDataItem)
       {

           return _dataBase.ExecuteQuery(sqlDataItem);
       }

        public bool ExecuteNonQueryArray(SqlDataItemArray arySqlDataItem)
       {
           return _dataBase.ExecuteNonQueryArray(arySqlDataItem);
       }

        public string ExecuteScalar(SqlDataItem sqlDataItem)
       {
           return _dataBase.ExecuteScalar(sqlDataItem);
       }

        public bool RecordExists(SqlDataItem sqlDataItem)
       {
           return _dataBase.RecordExists(sqlDataItem);
       }
       public string GetCurrentDatabaseName()
       {
           return _dataBase.GetCurrentDatabaseName();
       }
       public DataSet GetDBQuery(string databaseName)
       {
           return _dataBase.GetDBQuery(databaseName);
       }

       public DataSet GetDTColumnsQuery(string tableName, string databaseName)
       {
           return _dataBase.GetDTColumnsQuery(tableName, databaseName);
       }
       //public string GetSlXml(SqlDataItem sqlDataItem)
       //{
       //    DataSet ds= _dataBase.ExecuteQuery(sqlDataItem);
       //    return Connector.ToXml(ds);
       //}
       public string GetXml(SqlDataItem sqlDataItem)
       {
           DataSet ds = _dataBase.ExecuteQuery(sqlDataItem);
           return ds.GetXml();
       }
       public string GetDBDateTime()
       {
           return _dataBase.GetDBDateTime();
       }
       #region 获得一个空结构
       public DataSet GetBlankDataSet(List<string> tableNames)
       {
          return GetBlankDataSet(tableNames,null);
       }
       public DataSet GetBlankDataSet(List<string> tableNames, string databaseName)
       {
           return _dataBase.GetBlankDataSet(tableNames,databaseName);
       }
       #endregion

       #region 更新数据集
       public void UpdateDataSetWithTranse(DataSet dataSet)
       {
           UpdateDataSetWithTranse(dataSet, null);
       }
       /// <summary>
       /// 更新数据集，启动数据库事务
       /// </summary>
       /// <param name="ds">数据集</param>
       /// <param name="databaseName">数据库名</param>
       public void UpdateDataSetWithTranse(DataSet dataSet, string databaseName)
       {
           _dataBase.UpdateDataSetWithTranse(dataSet, databaseName);
       }

       #endregion

    }
}
